﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace JQuery
{
    public static partial class Extensions
    {
        public static string ResolveUrl(this HttpApplication Context, string RelativeUrl)
        {
            if (RelativeUrl.IsNullOrEmpty() || RelativeUrl.IndexOf("://") != -1)
                return RelativeUrl; // we return absolute paths no need to figure out the application root.

            if (RelativeUrl.StartsWith("~"))
            {
                if (Context == null)
                    throw new ArgumentNullException("Context");

                return String.Format("{0}{1}", HttpRuntime.AppDomainAppVirtualPath, RelativeUrl.Substring(2));
            }

            return RelativeUrl;
        }

        public static string MapFilePath(this HttpApplication Context, string RelativeUrl)
        {
            string Path = Context.ResolveUrl(RelativeUrl);

            if (Path.IndexOf("://") != -1)
            {
                Path = Path.Substring(Path.IndexOf("://") + 3);
            }

            return String.Format("{0}{1}", HttpRuntime.AppDomainAppPath, Path.Substring(1).Replace("/", "\\"));
        }
    }
}
